package com.magic.smartcity.view.more.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.magic.smartcity.R
import com.magic.smartcity.beans.ActivityRotation
import com.magic.smartcity.databinding.FragmentActivityHomeBinding
import com.magic.smartcity.view.more.adapters.ActivityHomeBannerAdapter
import com.magic.smartcity.view.more.adapters.ActivityHomeCategoryAdapter
import com.magic.smartcity.view.more.adapters.ActivityHomeListAdapter
import com.magic.smartcity.view.more.viewmodels.ActivityViewModel
import com.youth.banner.Banner

class ActivityHomeFragment : Fragment() {

    lateinit var binding: FragmentActivityHomeBinding
    var bannerList = mutableListOf<ActivityRotation>()

    val bannerAdapter=ActivityHomeBannerAdapter(bannerList)
    val categoryAdapter= ActivityHomeCategoryAdapter()
    val activitylistAdapter=ActivityHomeListAdapter()

    val viewModel by lazy {
        ViewModelProvider(this).get(ActivityViewModel::class.java)
    }

    /**
     * 请求数据
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.getActivityRotationImages()
        viewModel.getActivityCategory()
        viewModel.getActivityList()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentActivityHomeBinding.inflate(layoutInflater)
        initView()

        return binding.root
    }

    /**
     * 初始化组件，并初始化组件数据
     */
    private fun initView() {
        //轮播图数据设置
        binding.banner.setAdapter(bannerAdapter)
        viewModel.rotaionList.observe(viewLifecycleOwner){
            bannerAdapter.setDatas(it)
        }
        //分类数据设置
        binding.categoryRy.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        binding.categoryRy.adapter = categoryAdapter
        viewModel.category.observe(viewLifecycleOwner){
            categoryAdapter.setData(it)
        }
        //活动列表设置
        binding.listRy.adapter = activitylistAdapter
        binding.listRy.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        viewModel.activityList.observe(viewLifecycleOwner){
            activitylistAdapter.setData(it)
        }
    }

}